package mt.client;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

import mt.Order;

/**
 * ListUpdater will permit to the incoming messages from the Server to update the JTables and  Lists in the Graphic Interface.
 * Purchase Orders are added in the Purchase Order JTableModel.
 * Sell Orders are added in the Sell Order JTableModel.
 * Satisfied orders of the current client are added in History.
 * ListUpdater is a Singleton Class.
 * @author tom93d
 *
 */
public final class ListUpdater {
	
	/*
	 * Singleton class
	 */
	
	private List<Order> sellList, purchaseList;
	private JTableModel history;
	private String nickName;
	
	private static ListUpdater instance = null;
	
	private ListUpdater() {
		
	}
	/**
	 * This method is the class constructor for the singleton Object ListUpdater.
	 * @param sellList
	 * @param purchaseList
	 * @param history
	 * @param nickName
	 * @return The only existing instance of ListUpdater.
	 */
	public static ListUpdater getInstance(List<Order> sellList, List<Order> purchaseList, JTableModel history, String nickName) {
		if(instance == null) {
			instance = new ListUpdater();
			instance.purchaseList = purchaseList;
			instance.sellList = sellList;
			instance.history = history;
			instance.nickName = nickName;
		}
		return instance;
	}
	
	List<Order> getSellList() {
		return this.sellList;
	}
	
	List<Order> getPurchaseList() {
		return this.purchaseList;
	}
	
	/**
	 * updateLists method will permit to the incoming messages from the Server to update the JTables and  Lists in the Graphic Interface.
	 * Purchase Orders are added in the Purchase Order JTableModel.
	 * Sell Orders are added in the Sell Order JTableModel.
	 * Satisfied orders of the current client are added in History.
	 * @param order
	 */
	void updateLists(Order order) {
		java.util.Iterator<Order> i;
		Order o;
		if(order != null) {
			if(order.isSellOrder()) {
				i = purchaseList.iterator();
				while(i.hasNext()) {
					o = i.next();
					if(o.getStock().equals(order.getStock()) && o.getPricePerUnit() == order.getPricePerUnit()) {
						if(o.getNumberOfUnits() == order.getNumberOfUnits()) {
							updateHistory(o);
							updateHistory(order);
							purchaseList.remove(o);
							return;
						} else if(o.getNumberOfUnits() > order.getNumberOfUnits()) {
							updateHistory(order);
							o.setNumberOfUnits(o.getNumberOfUnits()-order.getNumberOfUnits());
							return;
						} else {
							/*
							 * if sellorder is bigger 
							 */
							updateHistory(o);
							order.setNumberOfUnits(order.getNumberOfUnits()-o.getNumberOfUnits());
							purchaseList.remove(o);
						}
					}
				}
				sellList.add(order);
			} else { //order.isBuyOrder()
				i = sellList.iterator();
				while(i.hasNext()) {
					o = i.next();
					if(o.getStock().equals(order.getStock()) && o.getPricePerUnit() == order.getPricePerUnit()) {
						if(o.getNumberOfUnits() == order.getNumberOfUnits()) {
							updateHistory(o);
							updateHistory(order);
							sellList.remove(o);
							return;
						} else if(o.getNumberOfUnits() > order.getNumberOfUnits()) {
							updateHistory(order);
							o.setNumberOfUnits(o.getNumberOfUnits()-order.getNumberOfUnits());
							return;
						} else {
							/*
							 * if buyorder is bigger 
							 */
							updateHistory(o);
							order.setNumberOfUnits(order.getNumberOfUnits()-o.getNumberOfUnits());
							sellList.remove(o);
						}
					}
				}
				purchaseList.add(order);
			}
		}
	}
	
	/**
	 * UpdateHistory is called to modify the History JTableModel,
	 * It will modify the table only if the order client is the current client.
	 * @param o
	 */
	private void updateHistory(Order o) {
		if(o.getNickname().equals(this.nickName)) {
			history.addEntry(o);
		}
	}

}
